<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Communication</em>
</p>
<p class='vspace'></p><h3>Graph</h3>
<p>This example shows you how to send a byte of data from the MSP430 LaunchPad to a personal computer and graph the result. This is called serial communication because the connection appears to both the LaunchPad and the computer as a serial port, even though it may actually use a USB cable.ou can use the Energia serial monitor to view the sent data, or it can be read by Processing.

</p>
<p class='vspace'></p><div  class='BOM' >
<h3>Hardware Required</h3>
<ul><li>MSP-EXP430G2 LaunchPad
</li><li>a variable resistor, like a potentiometer
</li></ul><p class='vspace'></p></div>
<h3>Relevant Groundwork</h3>
<p class='vspace'></p><p>We can connect analog sensors to the MSP430 through the 8 analog read pins.  You can view the pin mapping <a href='PinMapping.html'>here</a> for the mapping for the particular MSP430 device you are using on the LaunchPad.  The Launchpad will read the analog signal coming from the device and send it to the analog to digital converter in the microcontroller.  We can read these digital values in the Serial Monitor in Energia. 
</p>
<p>Connect a potentiometer or other analog sensor to analog input 3.
</p>
<p class='vspace'></p><h3>Circuit</h3>
<div  class='circuit' >
<p class='vspace'></p><div><img height='400px' src='../reference/img/Graph_bb.png' alt='' title='' /></div>
<p class='vspace'></p><p><span style='font-size:83%'>image developed using <a class='urllink' href='http://www.fritzing.org' rel='nofollow'>Fritzing</a>. For more circuit examples, see the <a class='urllink' href='http://fritzing.org/projects/' rel='nofollow'>Fritzing project page</a> </span>
</p>
<p class='vspace'></p></div>
<p class='vspace'></p><h3>Schematic</h3>
<div  class='circuit' >
<div><img width='400px' src='../reference/img/Graph_schem.png' alt='' title='' /></div>
</div>
<p class='vspace'></p><h3>Code Explanation</h3>
<h4>Energia</h4>
<p>The first step is to set up Serial communication between the LaunchPad and the Serial monitor.  We accomplish this through the Serial.begin() function.  We set up communication to 9600 bits of data per second.  In the loop() function, we use Serial.println() to print out the output to the Serial monitor in Energia.  analogRead(A3) inside the Serial.println() looks at the analog signal being read from the potentiometer connected to analog pin 3.  We add a delay at the end of this function to stabilize the analog to digital converter after we read the last reading. 
</p>
<h4>Processing</h4>
<p>It is important to note that when using Processing, you have to first flash your Energia code onto the LaunchPad.  Processing will be calling upon the device connected to the COM port that your LaunchPad is connected to in order to read the value of the potentiometer to output to the graph generated by the Processing code.
</p>
<p class='vspace'></p><h3>Code</h3>
<pre>
/*
  Graph

  A simple example of communication from the Launchpad to the computer:
  the value of analog input 0 is sent out the serial port. We call this "serial"
  communication because the connection appears to both the Launchpad and the
  computer as a serial port, even though it may actually use
  a USB cable. Bytes are sent one after another (serially) from the Launchpad
  to the computer.

  You can use the Energia serial monitor to view the sent data, or it can
  be read by Processing or another program capable of reading 
  data from a serial port. The Processing code below graphs the data received 
  so you can see the value of the analog input changing over time.

  The circuit:
  Any analog input sensor can be attached to an analog pin.  
  For this example, a potentiometer is attached to analog pin 3. 

  created 2006
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe and Scott Fitzgerald
  Modified 15 April 2013
  By Sean Alvarado

  Hardware Required:
  * MSP-EXP430G2 LaunchPad
  * Potentiometer
  
  This example code is in the public domain.

*/

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  // send the value of analog input 3:
  Serial.println(analogRead(A3));
  // wait a bit for the analog-to-digital converter 
  // to stabilize after the last reading:
  delay(1000);
}

/* 
// Processing code for this example

// Graphing sketch


// This program takes ASCII-encoded strings
// from the serial port at 9600 baud and graphs them. It expects values in the
// range 0 to 1023, followed by a newline, or newline and carriage return

// Created 20 Apr 2005
// Updated 18 Jan 2008
// by Tom Igoe
// modified 16 April 2013
// by Sean Alvarado
// This example code is in the public domain.

import processing.serial.*;

Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph

void setup () {
  // set the window size:
  size(400, 300); 

  // List all the available serial ports
  println(Serial.list());
  // Open whatever port is the one you're using for the Launchpad.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(0);
}
void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int and map to the screen height:
    float inByte = float(inString); 
    inByte = map(inByte, 0, 1023, 0, height);

    // draw the line:
    stroke(127,34,255);
    line(xPos, height, xPos, height - inByte);

    // at the edge of the screen, go back to the beginning:
    if (xPos >= width) {
      xPos = 0;
      background(0); 
    } 
    else {
      // increment the horizontal position:
      xPos++;
    }
  }
}
*/
</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>

<p class='vspace'></p><h3>See Also:</h3>
<ul><li><a class='wikilink' href='Serial.html'>serial</a>()
</li><li><a class='wikilink' href='Serial_Read.html'>serial.read</a>()
</li><li><a class='wikilink' href='AnalogRead.html'>analogRead</a>()
</li><li><a class='wikilink' href='AnalogWrite.html'>analogWrite</a>()
</li><li><a class='wikilink' href='Tutorial_Dimmer.html'>Dimmer</a> - move the mouse to change the brightness of an LED.
</li><li><a class='wikilink' href='Tutorial_PhysicalPixel.html'>Physical Pixel</a> - turn an LED on and off by sending data from Processing.
</li><li><a class='wikilink' href='Tutorial_VirtualColorMixer.html'>Virtual Color Mixer</a> - send multiple variables from a LaunchPad to the computer and read them in Processing. 
</li><li><a class='wikilink' href='Tutorial_SerialCallResponse.html'>Serial Call Response</a> - send multiple variables using a call and response (handshaking) method.
</li><li><a class='wikilink' href='Tutorial_SerialCallResponseASCII.html'>Serial Call and Response ASCII</a> - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
</li><li><a class='wikilink' href='Tutorial_SwitchCase2.html'>Serial Input (Switch (case) Statement)</a> - How to take different actions based 0n characters received by the serial port. 
</li></ul><p class='vspace'></p>
<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
